package com.karst.webnotes.chapter04
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.view.BasicView;
	
	internal class FlipMaterialExample extends Sprite
	{
		public function FlipMaterialExample()
		{
			super();
			createView() ;
		}
		
		private var view : BasicView = null ;
		private var front : BitmapFileMaterial = null ;
		private var back : BitmapFileMaterial = null ;
		private var top : BitmapFileMaterial = null ;
		private var bottom : BitmapFileMaterial = null ;
		private var left : BitmapFileMaterial = null ;
		private var right : BitmapFileMaterial = null ;
		private var materials : MaterialsList = null ;
		private var cube : Cube = null ;
		private function createView() : void
		{
			if( view == null )
			{
				view = new BasicView( 0, 0, true, true ) ;
				view.buttonMode = true ;
				view.addEventListener( Event.ENTER_FRAME, rateHandler ) ;
			}
			
			if( front == null )
			{
				front = new BitmapFileMaterial( "01.jpg" ) ;
//				front.doubleSided = true ;  //将材质设为双面属性，两个面都能看到
			}
			
			if( back == null )
			{
				back = new BitmapFileMaterial( "01.jpg" ) ;
//				back.opposite = true ;  //将材质设为反面模式，只能看到背面。看正面时是透明的
			}
			
			if( top == null )
			{
				top = new BitmapFileMaterial( "01.jpg" ) ;
//				top.doubleSided = true ;
			}
			
			if( bottom == null )
			{
				bottom = new BitmapFileMaterial( "01.jpg" ) ;
//				bottom.opposite = true ;
			}
			
			if( left == null )
			{
				left = new BitmapFileMaterial( "01.jpg" ) ;
//				left.doubleSided = true ;
			}
			
			if( right == null )
			{
				right = new BitmapFileMaterial( "01.jpg" ) ;
//				right.opposite = true ;
			}
			
			if( materials == null )
			{
				materials = new MaterialsList() ;
				materials.addMaterial( front, "front" ) ;
				materials.addMaterial( back, "back" ) ;
				materials.addMaterial( left, "left" ) ;
				materials.addMaterial( right, "right" ) ;
				materials.addMaterial( top, "top" ) ;
				materials.addMaterial( bottom, "bottom" ) ;
			}
			
			if( cube == null )
			{
				cube = new Cube( materials, 400, 400, 450, 2 ) ;
			}
			
			addChild( view ) ;
			view.scene.addChild( cube ) ;
		}
		
		private function rateHandler( event : Event ) : void
		{
			if( view )
				view.singleRender() ;
			if( cube )
			{
				cube.localRotationX += 2 ;
				cube.localRotationY-- ;
				cube.localRotationZ-- ;
			}
		}
	}
}